//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "GeneralizedBlackScholesProcess.h"
using namespace Cephei::QL::Processes;
#include <gen/QL/Termstructures/Volatility/Equityfx/BlackVolTermStructure.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Termstructures/Volatility/Equityfx/LocalVolTermStructure.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/StochasticProcess1D.h>
using namespace Cephei::QL::Termstructures::Volatility::Equityfx;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
using namespace Cephei::QL::Math;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Processes::CGeneralizedBlackScholesProcess::CGeneralizedBlackScholesProcess (boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>& childNative, Object^ owner) : CStochasticProcess1D(CGeneralizedBlackScholesProcess::typeid)
{
#ifdef HANDLE
	_phGeneralizedBlackScholesProcess = NULL;
#endif
	_ppGeneralizedBlackScholesProcess = &childNative;
    _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppGeneralizedBlackScholesProcess));
}
Cephei::QL::Processes::CGeneralizedBlackScholesProcess::CGeneralizedBlackScholesProcess (QuantLib::GeneralizedBlackScholesProcess& childNative, Object^ owner) : CStochasticProcess1D(CGeneralizedBlackScholesProcess::typeid)
{
#ifdef HANDLE
	_phGeneralizedBlackScholesProcess = NULL;
#endif
	_ppGeneralizedBlackScholesProcess = new boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess> (&childNative);
    _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppGeneralizedBlackScholesProcess));
    _GeneralizedBlackScholesProcessOwner = owner;
    _StochasticProcess1DOwner = owner;
}

Cephei::QL::Processes::CGeneralizedBlackScholesProcess::CGeneralizedBlackScholesProcess (CGeneralizedBlackScholesProcess^ copy) : CStochasticProcess1D(CGeneralizedBlackScholesProcess::typeid)
{
#ifdef HANDLE
	_phGeneralizedBlackScholesProcess = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppGeneralizedBlackScholesProcess = new boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess> (copy->GetShared());
        _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppGeneralizedBlackScholesProcess));
    }
}
Cephei::QL::Processes::CGeneralizedBlackScholesProcess::CGeneralizedBlackScholesProcess (System::Type^ t) : CStochasticProcess1D(CGeneralizedBlackScholesProcess::typeid)
{
#ifdef HANDLE
	_phGeneralizedBlackScholesProcess = NULL;
#endif
	if (!t->IsSubclassOf(CGeneralizedBlackScholesProcess::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Processes::CGeneralizedBlackScholesProcess::CGeneralizedBlackScholesProcess (QuantLib::Handle<QuantLib::GeneralizedBlackScholesProcess>& childNative, Object^ owner)  : CStochasticProcess1D(CGeneralizedBlackScholesProcess::typeid)
{
	_phGeneralizedBlackScholesProcess = &childNative;
	_ppGeneralizedBlackScholesProcess = &static_cast<boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>>(childNative.currentLink());
    _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppGeneralizedBlackScholesProcess));
    _GeneralizedBlackScholesProcessOwner = owner;
}
Cephei::QL::Processes::CGeneralizedBlackScholesProcess::CGeneralizedBlackScholesProcess (QuantLib::Handle<QuantLib::GeneralizedBlackScholesProcess> childNative)  : CStochasticProcess1D(CGeneralizedBlackScholesProcess::typeid)
{
	_phGeneralizedBlackScholesProcess = &childNative;
	_ppGeneralizedBlackScholesProcess = &static_cast<boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>>(childNative.currentLink());
    _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppGeneralizedBlackScholesProcess));
}
#endif
#ifdef STRUCT
Cephei::QL::Processes::CGeneralizedBlackScholesProcess::CGeneralizedBlackScholesProcess (QuantLib::GeneralizedBlackScholesProcess childNative)  : CStochasticProcess1D(CGeneralizedBlackScholesProcess::typeid)
{
#ifdef HANDLE
	_phGeneralizedBlackScholesProcess = NULL;
#endif
	_ppGeneralizedBlackScholesProcess = new boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess> (new QuantLib::GeneralizedBlackScholesProcess (childNative));
    _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppGeneralizedBlackScholesProcess));
}
#endif

Cephei::QL::Processes::CGeneralizedBlackScholesProcess::~CGeneralizedBlackScholesProcess ()
{
    if (_ppGeneralizedBlackScholesProcess != NULL)
    {
	    delete _ppGeneralizedBlackScholesProcess;
        _ppGeneralizedBlackScholesProcess = NULL;
    }
}
Cephei::QL::Processes::CGeneralizedBlackScholesProcess::!CGeneralizedBlackScholesProcess ()
{
    if (_ppGeneralizedBlackScholesProcess != NULL)
    {
	    delete _ppGeneralizedBlackScholesProcess;
    }
}
QuantLib::GeneralizedBlackScholesProcess& Cephei::QL::Processes::CGeneralizedBlackScholesProcess::GetReference ()
{
    if (_ppGeneralizedBlackScholesProcess == NULL) throw gcnew NativeNullException ();
	return **_ppGeneralizedBlackScholesProcess;
}
boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>& Cephei::QL::Processes::CGeneralizedBlackScholesProcess::GetShared ()
{
    if (_ppGeneralizedBlackScholesProcess == NULL) throw gcnew NativeNullException ();
	return *_ppGeneralizedBlackScholesProcess;
}
QuantLib::GeneralizedBlackScholesProcess* Cephei::QL::Processes::CGeneralizedBlackScholesProcess::GetPointer ()
{
    if (_ppGeneralizedBlackScholesProcess == NULL) throw gcnew NativeNullException ();
	return &**_ppGeneralizedBlackScholesProcess;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::GeneralizedBlackScholesProcess>& Cephei::QL::Processes::CGeneralizedBlackScholesProcess::GetHandle ()
{
	if (_phGeneralizedBlackScholesProcess == NULL)
	{
		_phGeneralizedBlackScholesProcess = new Handle<QuantLib::GeneralizedBlackScholesProcess> (*_ppGeneralizedBlackScholesProcess);
	}
	return *_phGeneralizedBlackScholesProcess;
}
#endif
bool Cephei::QL::Processes::CGeneralizedBlackScholesProcess::HasNative () 
{
	return (_ppGeneralizedBlackScholesProcess != NULL);
}

Double Cephei::QL::Processes::CGeneralizedBlackScholesProcess::Apply (Double x0, Double dx)
{
    try
    {
        QuantLib::Real _x0 = (QuantLib::Real)ValueHelper::Convert (x0);
        QuantLib::Real _dx = (QuantLib::Real)ValueHelper::Convert (dx);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppGeneralizedBlackScholesProcess)->apply ( _x0,  _dx );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Volatility::Equityfx::IBlackVolTermStructure^ Cephei::QL::Processes::CGeneralizedBlackScholesProcess::BlackVolatility::get ()
{
    try
    {
    	Handle<QuantLib::BlackVolTermStructure>& _rv = (Handle<QuantLib::BlackVolTermStructure>&)(*_ppGeneralizedBlackScholesProcess)->blackVolatility ( );   
        Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVolTermStructure^ _nrv = gcnew Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVolTermStructure (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CGeneralizedBlackScholesProcess::Diffusion (Double t, Double x)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppGeneralizedBlackScholesProcess)->diffusion ( _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::IYieldTermStructure^ Cephei::QL::Processes::CGeneralizedBlackScholesProcess::DividendYield::get ()
{
    try
    {
    	Handle<QuantLib::YieldTermStructure>& _rv = (Handle<QuantLib::YieldTermStructure>&)(*_ppGeneralizedBlackScholesProcess)->dividendYield ( );   
        Cephei::QL::Termstructures::CYieldTermStructure^ _nrv = gcnew Cephei::QL::Termstructures::CYieldTermStructure (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CGeneralizedBlackScholesProcess::Drift (Double t, Double x)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppGeneralizedBlackScholesProcess)->drift ( _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Volatility::Equityfx::ILocalVolTermStructure^ Cephei::QL::Processes::CGeneralizedBlackScholesProcess::LocalVolatility::get ()
{
    try
    {
    	Handle<QuantLib::LocalVolTermStructure>& _rv = (Handle<QuantLib::LocalVolTermStructure>&)(*_ppGeneralizedBlackScholesProcess)->localVolatility ( );   
        Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolTermStructure^ _nrv = gcnew Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolTermStructure (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::IYieldTermStructure^ Cephei::QL::Processes::CGeneralizedBlackScholesProcess::RiskFreeRate::get ()
{
    try
    {
    	Handle<QuantLib::YieldTermStructure>& _rv = (Handle<QuantLib::YieldTermStructure>&)(*_ppGeneralizedBlackScholesProcess)->riskFreeRate ( );   
        Cephei::QL::Termstructures::CYieldTermStructure^ _nrv = gcnew Cephei::QL::Termstructures::CYieldTermStructure (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IQuote^ Cephei::QL::Processes::CGeneralizedBlackScholesProcess::StateVariable::get ()
{
    try
    {
    	Handle<QuantLib::Quote>& _rv = (Handle<QuantLib::Quote>&)(*_ppGeneralizedBlackScholesProcess)->stateVariable ( );   
        Cephei::QL::CQuote^ _nrv = gcnew Cephei::QL::CQuote (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CGeneralizedBlackScholesProcess::Time (DateTime d)
{
    try
    {
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d);
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppGeneralizedBlackScholesProcess)->time ( _d );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ Cephei::QL::Processes::CGeneralizedBlackScholesProcess::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppGeneralizedBlackScholesProcess)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CGeneralizedBlackScholesProcess::X0::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppGeneralizedBlackScholesProcess)->x0 ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CGeneralizedBlackScholesProcess::Evolve (Double t0, Double x0, Double dt, Double dw)
{
    try
    {
        QuantLib::Time _t0 = (QuantLib::Time)ValueHelper::Convert (t0);
        QuantLib::Real _x0 = (QuantLib::Real)ValueHelper::Convert (x0);
        QuantLib::Time _dt = (QuantLib::Time)ValueHelper::Convert (dt);
        QuantLib::Real _dw = (QuantLib::Real)ValueHelper::Convert (dw);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppGeneralizedBlackScholesProcess)->evolve ( _t0,  _x0,  _dt,  _dw );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CGeneralizedBlackScholesProcess::Expectation (Double t0, Double x0, Double dt)
{
    try
    {
        QuantLib::Time _t0 = (QuantLib::Time)ValueHelper::Convert (t0);
        QuantLib::Real _x0 = (QuantLib::Real)ValueHelper::Convert (x0);
        QuantLib::Time _dt = (QuantLib::Time)ValueHelper::Convert (dt);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppGeneralizedBlackScholesProcess)->expectation ( _t0,  _x0,  _dt );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

